package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Member;
import com.atguigu.service.MemberService;
import com.atguigu.utils.RedisMessageConstant;
import com.atguigu.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

/**
 * @author fenge
 */
@RestController
@RequestMapping("/login")
public class LoginContorller {


    @Autowired
    JedisPool jedisPool;

    @Reference
    MemberService memberService;

    @RequestMapping("/check")
    public Result check(@RequestBody Map loginInfo){
        String telephone = loginInfo.get("telephone").toString();
        Member member = memberService.getMemberByTel(telephone);
    if (member==null){
        member = new Member();
        member.setPhoneNumber(telephone);
        member.setRegTime(new Date());
        memberService.add(member);
    }









        String validateCode = loginInfo.get("validateCode").toString();
        String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        if (redisCode==null||!redisCode.equals(validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);

    }
}
